package com.muyu.danmo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCollectMaterial;
import com.muyu.danmo.domain.DanmoCollectShellfish;
import com.muyu.danmo.service.IDanmoCollectMemberService;
import com.muyu.danmo.service.IDanmoCollectShellfishService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 拾贝信息Controller
 *
 * @author yangle
 * @date 2024-10-30
 */
@Log4j2
@RestController
@RequestMapping("/collectShellfish")
public class DanmoCollectShellfishController extends BaseController
{
    @Autowired
    private IDanmoCollectShellfishService danmoCollectShellfishService;

    @Autowired
    private IDanmoCollectMemberService danmoCollectMemberService;


    /**
     * 查询拾贝信息列表
     */
//    @RequiresPermissions("system:collectShellfish:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoCollectShellfish danmoCollectShellfish)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        PageHelper.startPage(danmoCollectShellfish.getPageNum(),danmoCollectShellfish.getPageSize());
        List<DanmoCollectShellfish> list = danmoCollectShellfishService.selectDanmoCollectShellfishList(danmoCollectShellfish);
        PageInfo<DanmoCollectShellfish> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    /**
     * 导出拾贝信息列表
     */
//    @RequiresPermissions("system:collectShellfish:export")
    @Log(title = "拾贝信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoCollectShellfish danmoCollectShellfish)
    {
        List<DanmoCollectShellfish> list = danmoCollectShellfishService.selectDanmoCollectShellfishList(danmoCollectShellfish);
        ExcelUtil<DanmoCollectShellfish> util = new ExcelUtil<DanmoCollectShellfish>(DanmoCollectShellfish.class);
        util.exportExcel(response, list, "拾贝信息数据");
    }

    /**
     * 获取拾贝信息详细信息
     */
//    @RequiresPermissions("system:collectShellfish:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return danmoCollectShellfishService.selectDanmoCollectShellfishById(id);
    }

    /**
     * 新增拾贝信息
     */
//    @RequiresPermissions("system:collectShellfish:add")
    @Log(title = "拾贝信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoCollectShellfish danmoCollectShellfish)
    {
        return  danmoCollectShellfishService.insertDanmoCollectShellfish(danmoCollectShellfish);
    }

    /**
     * 修改拾贝信息
     */
//    @RequiresPermissions("system:collectShellfish:edit")
    @Log(title = "拾贝信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody DanmoCollectShellfish danmoCollectShellfish)
    {
        return danmoCollectShellfishService.updateDanmoCollectShellfish(danmoCollectShellfish);
    }


    /**
     * 拾贝下架
     */
//    @RequiresPermissions("system:collectShellfish:edit")
//    @Log(title = "拾贝下架", businessType = BusinessType.UPDATE)
    @GetMapping("getinfo/{id}")
    public Result removed(@PathVariable("id") Long id)
    {
        return danmoCollectShellfishService.updateDanmoCollectShellfishById(id);
    }

    /**
     * 拾贝重新上架
     */
//    @RequiresPermissions("system:collectShellfish:edit")
//    @Log(title = "拾贝下架", businessType = BusinessType.UPDATE)
    @GetMapping("listed/{id}")
    public Result listed (@PathVariable("id") Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        boolean b = danmoCollectMemberService.selectDanmoCollectMemberById(loginUser.getUserid());
        if (!b) {
            return Result.success(1,"您的会员已过期，请重新开通");
        }
        return danmoCollectShellfishService.updateDanmoCollectShellfishListedById(id);
    }



    /**
     * 删除拾贝信息
     */
//    @RequiresPermissions("system:collectShellfish:remove")
    @Log(title = "拾贝信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return danmoCollectShellfishService.deleteDanmoCollectShellfishByIds(ids);
    }

    /**
     * 查看自己的作品
     */
    @PostMapping("/myShellfish")
    public Result myShellfish()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userId=loginUser.getUserid();
        List<DanmoCollectShellfish> danmoCollectShellfishList=danmoCollectShellfishService.myShellfish(userId);
        return success(danmoCollectShellfishList);
    }

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 查看自己拾贝收藏
     */
    @PostMapping()
    public Result collection(@RequestBody DanmoCollectMaterial danmoCollectMaterial) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoCollectMaterial.setUserId(loginUser.getUserid());
        return danmoCollectShellfishService.selectDanmoCollectMaterialCollection();
    }



    /**
     * 从作品集选取
     */
    @GetMapping("/selectDanmoPortfolio/{id}")
    public Result selectDanmoPortfolio(@PathVariable("id") Long id)
    {
        return Result.success(danmoCollectShellfishService.selectDanmoPortfolioById(id));
    }
}
